<?php


class PartageManager {

   
    public function __construct(Mypdo $db){
        $this->db= $db;
    }



    public function add($part = array()){
        $req = $this->db->prepare('INSERT INTO partage ( id_pers, id_loca ) VALUES ( :id_pers, :id_loca )');
        $req->bindValue(':id_pers', $part['id_pers'], PDO::PARAM_INT);
        $req->bindValue(':id_loca', $part['id_loca'], PDO::PARAM_INT);

        $req->execute();

    }

    public function  delPartByIdPers($id_pers){
    $sql = 'DELETE FROM immoloc.partage WHERE id_pers = :id_pers';
    $req = $this->db->prepare($sql);
    $req->bindValue(':id_pers', $id_pers, PDO::PARAM_INT);

    $req->execute();

}

    public function  delPartByIdPersLoca($id_pers, $id_loca){
        $sql = 'DELETE FROM immoloc.partage WHERE id_pers = :id_pers AND id_loca = :id_loca';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_pers', $id_pers, PDO::PARAM_INT);
        $req->bindValue(':id_loca', $id_loca, PDO::PARAM_INT);

        $req->execute();

    }

    public function getIdPersByIdLoca($id_loca){
        $sql = 'SELECT id_pers FROM partage WHERE id_loca = :id_loca';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_loca', $id_loca, PDO::PARAM_INT);

        $req->execute();
        return $req->fetchAll(PDO::FETCH_ASSOC);
    }

    public function delPartByIdImmeu($id_immeu)
    {
        $sql = 'DELETE FROM partage WHERE id_loca IN
                (SELECT id_loca FROM occupe o
                INNER JOIN location l ON o.id_loc = l.id_loc
                WHERE id_immeu = :id_immeu)';
        $req = $this->db->prepare($sql);

        $req->bindValue(':id_immeu', $id_immeu, PDO::PARAM_INT);

        $req->execute();

    }

}
